import axios from 'axios';


/**
 * @Description：获取系统缓存的字典值\ sessionID
 * @author：myd
 * @date : 2020/03/13
 * @remark : JSON.parse()【从一个字符串中解析出json对象】/ JSON.stringify()【从一个对象中解析出字符串】
 */
export function getAllDic(feedback){
    return new Promise((resolve, reject) => {
        // axios.getAllDic('/auth/dict/selectDict',{dict_status:'0'})
        this.$post('/auth/dict/queryDict', {dict_status: '1'}).then(res => {
            var dicString = JSON.stringify(res);
            sessionStorage.setItem('dicString', dicString); // => 缓存dicString字符串
            if (feedback) {
                feedback();
            }
        }, err => {
            reject(err)
        })
    })
}

/**
 * @description 根据字典的CodeType获取相应的字典值
 * * @author myd
 * @date 2020/03/13
 * @returns {Array}
 */
export function getDict() {
    var temp = [];
    var dicString = sessionStorage.getItem('dicString'); // => 返回dicString
    if(dicString != null || dicString != undefined){
        var ob = JSON.parse(dicString);
        for(var i = 0; i < ob.length;i++){
            if(ob[i].dict_type == arguments[0]){
                temp.push(ob[i]);
            }
        }
    }
    return temp;
}

/**
 * 列表时间转化
 * @param h
 * @param params
 * @param field
 * @returns {string}
 */
export function rederDateYMD(h, params, field) {
    return h('span', {}, params.row[field] ? this.$dateFormat(params.row[field]).format('YYYY-MM-DD') : "");
}


/**
 * 时间转化
 * @param date
 * @returns {string}
 */
export function dateFormatYMD(date) {
    return this.$dateFormat(date).format('YYYY-MM-DD');
}
/**
 * @Description:字典名称转为字典值
 * @author      zdc
 * @return
 * @exception
 * @date        2020-08-31 10:54
 */
export function dictName2Code(type, name) {
    let value = name;
    let dics = getDict(type);
    if (dics) {
        dics.forEach((r) => {
            if (r['dict_name'] === name) {
                value = r['dict_code'];
            }
        });
    }
    return value;
}
/**
 * @Description:字典值转为字典名称
 * @author      zdc
 * @return
 * @exception
 * @date        2020-08-31 10:55
 */
export function dictCode2Name(type, code) {
    let value = code;
    let dics = getDict(type);
    if (dics) {
        dics.forEach((r) => {
            if (r['dict_code'] === code) {
                value = r['dict_name'];
            }
        });
    }
    return value;
}

// add by zhaodecang 2020-09-25 10:16:全局表单校验
export function formValidate(formObj, callback) {
    if (formObj) {
        formObj.validate(valid => {
            if (valid) {
                callback(valid);
            } else {
                if (process.env.VUE_APP_GLOBAL_VALIDATOR_MODE && process.env.VUE_APP_GLOBAL_VALIDATOR_MESSAGE) {
                    this.$alert(process.env.VUE_APP_GLOBAL_VALIDATOR_MESSAGE, '系统提示', {confirmButtonText: '确定'});
                }
            }
        });
    }
}
